package top.codervan.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import top.codervan.yygh.common.result.Result;
import top.codervan.yygh.common.utils.MD5;
import top.codervan.yygh.hosp.service.HospitalSetService;
import top.codervan.yygh.model.hosp.HospitalSet;
import top.codervan.yygh.vo.hosp.HospitalSetQueryVo;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author: van
 * @Date: 2022/2/13 21:01
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {


    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 1、查询医院设置表所有信息
     * @return 返回结果
     */
    @ApiOperation(value = "获取所有的医院设置信息")
    @GetMapping("findAll")
    public Result findAllHospitalSet(){
        List<HospitalSet> hospitalSets = hospitalSetService.list();
        return Result.ok(hospitalSets);
    }

    /**
     * 逻辑删除医院设置
     * @param id 要删除的id
     * @return  返回结果
     */
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeHospSet(@PathVariable Long id){
        boolean flag = hospitalSetService.removeById(id);
        if(flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    /**
     * 条件查询带分页
     * @param current 当前页
     * @param limit 每页的数据条数
     * @param hospitalSetQueryVo 参数以对象的方式进行传递
     * @return 返回结果
     */
    @ApiOperation(value = "条件查询带分页医院设置")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHosSet(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo){
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        //进行空参数判断
        if(!StringUtils.isEmpty(hosname)){
            wrapper.like("hosname",hosname);
        }
        if(!StringUtils.isEmpty(hoscode)){
            wrapper.eq("hoscode",hoscode);
        }
        //调用方法实现分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        //返回结果
        return Result.ok(hospitalSetPage);
    }

    /**
     * 添加医院设置表信息
     * @param hospitalSet 前端传入的实体类数据
     * @return 返回处理结果
     */
    @ApiOperation(value = "添加医院设置表信息")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody(required = true) HospitalSet hospitalSet){
        //我们需要手动设置两个值，一是设置状态status，二是设置签名sign_key
        //设置状态 1为可用，0为不可用
        hospitalSet.setStatus(1);
        //设置签名密钥，使用MD5类进行加密
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        //最后调用方法保存数据
        boolean flag = hospitalSetService.save(hospitalSet);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 根据id查询医院设置
     * @param id 前端传入的id
     * @return  返回单个医院的设置结果
     */
    @ApiOperation(value = "根据id查询医院设置")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    /**
     * 更新医院设置表的信息
     * @param hospitalSet 前端传入的需要更新的值
     * @return 返回更新结果
     */
    @ApiOperation(value = "更新医院设置表的信息")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 批量删除医院设置
     * @param idList 传入要删除的医院的id集合
     * @return 删除的结果
     */
    @ApiOperation(value = "批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Long> idList){
        boolean flag = hospitalSetService.removeByIds(idList);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 医院设置的锁定和解锁
     * @param id 要设置的id
     * @param status 要设置的状态
     * @return 设置结果
     */
    @ApiOperation(value = "医院设置的锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        //根据id查出要修改状态的值
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置要修改成的状态
        hospitalSet.setStatus(status);
        //完成状态的设置
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if(flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    /**
     * 发送签名密钥
     * @param id 需要发送的id
     * @return 返回结果
     */
    @ApiOperation(value = "发送签名密钥")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable Long id){
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // TODO 发送短信进行验证 后续完善
        return Result.ok();
    }

}
